<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>注册</title>
    <link rel="stylesheet" href="css/common/reset.css">
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/reset-layui.css">
    <link rel="stylesheet" href="css/login.css">
    <link rel="stylesheet" href="//at.alicdn.com/t/font_848034_62n56i5y1xn.css">
</head>

<body>
    <div class="loginView">
        <div class="nav">
            <a href="" class="logo"></a>
            <div class="header-bar-menu">
                <a href="login.html">登录</a> | <a href="register.html">注册</a>
            </div>
        </div>
        <div class="loginPanel">
            <h2>用户注册</h2>
            <form action="" class="formLogin layui-form">
                <div>
                    <i class="iconfont icon-yonghuming"></i>
                    <input type="text" placeholder="用户名" id="username" lay-verify="uname" lay-verify="required"
                        autocomplete="off">
                </div>
                <div>
                    <i class="iconfont icon-mima"></i>
                    <input type="password" placeholder="密码" id="pwd" lay-verify="pwd" lay-verify="required">
                </div>
                <div>
                    <i class="iconfont icon-lr_password2"></i>
                    <input type="password" placeholder="确认密码" id="regPwd" lay-verify="regPwd" lay-verify="required">
                </div>
                <div>
                    <i class="iconfont icon-shouji"></i>
                    <input type="text" placeholder="请输入手机号" id="mobile"><span class="yzm" id="code">获取验证码</span>
                </div>
                <div>
                    <i class="iconfont icon-yanzhengma"></i>
                    <input type="text" placeholder="请输入短信验证码" id="writeCode" lay-verify="required">
                </div>
                <div>
                    <i class="iconfont icon-shouji"></i>
                    <input type="text" placeholder="请输入邀请码" id="inviteCode">
                </div>
                <input type="submit" value="注册" class="btn" lay-submit="" lay-filter="demo1">
            </form>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="layui/layui.js"></script>
    <!-- 获取验证码操作倒计时操作 -->
    <script>

        function _getCode() {
            var mobile = $("#mobile").val();
            var JdateJson = {
                "mobile": mobile
            }
            $.ajax({
                url: 'http://www.xiaofeixiangf.com:8888/v1/applet/code',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(JdateJson),
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    console.log(data);
                    localStorage.code = data.data;
                }
            })
        }
        var codes = $(".yzm");
        var validCode = true;
        codes.click(function () {
            //验证手机号码
            var phone = $('#mobile').val();
            var pattern = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
            var isPhone = 1;
            if (phone == '') {
                // alert('请输入手机号码');
                layer.msg('请输入手机号码');
                isPhone = 0;
                return;
            }
            if (!pattern.test(phone)) {
                alert('请输入正确的手机号码');
                isPhone = 0;
                return;
            }
            console.log(validCode, 111)
            var time = 60; //倒计时时间
            var that = $(this);
            if (validCode) {
                _getCode();
                validCode = false;
                $(this).addClass("code-send");
                var t = setInterval(function () {
                    time--;
                    if (time <= 0) {
                        window.clearInterval(t);
                        codes.text("重新获取");
                        validCode = true;
                        codes.removeClass("code-send");
                        that.css({ "disabled": false, cursor: 'pointer' });
                    } else {
                        codes.text(time + "秒");
                        that.css({ "disabled": true, cursor: 'not-allowed' });
                    }
                }, 1000);
            }
        });
    </script>
    <script>
        layui.use(['form', 'jquery', 'layer'], function () {
            var form = layui.form;
            var $ = layui.jquery;
            var layer = layui.layer;
            //自定义验证规则
            form.verify({
                uname: function (value, item) {
                    if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                        return '用户名不能有特殊字符';
                    }
                    if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                        return '用户名首尾不能出现下划线\'_\'';
                    }
                    if (/^\d+\d+\d$/.test(value)) {
                        return '用户名不能全为数字';
                    }
                },
                // , pwd: [
                //     /^[\S]{6,12}$/
                //     , '密码必须6到12位，且不能出现空格'
                // ],
                // regPwd: function (value) {
                //     //获取密码
                //     var pwd = $("#pwd").val();
                //     if (!new RegExp(pwd).test(value)) {
                //         return '两次输入的密码不一致';
                //     }
                // }
                pwd: [/(.+){6,12}$/, '密码必须6到12位'],
                regPwd: function (value) {
                    if (value != $("#pwd").val()) {
                        $("#regPwd").val("");
                        return '确认密码与密码不一致';
                    }
                },
            });
            //添加表单监听事件,提交注册信息
            form.on('submit(demo1)', function () {
                console.log("fjahkghak");
                var inviteCode = $("#inviteCode").val();
                console.log("hfakjghaklghagkja===========", inviteCode);
                var user = $('#username').val();
                var pwd = $('#pwd').val();
                var regPwd = $('#regPwd').val();
                var code = $("#writeCode").val();
                console.log(code);
                var _Code = localStorage.code;
                console.log(code);
                if (code === '') {
                    layer.msg('请输入验证码！');
                    return false;
                } else if (code == _Code) {
                    layer.msg('验证码正确');
                } else {
                    layer.msg('验证码错误！请重新输入！');
                    return false;
                }
                if (inviteCode === '') {
                    layer.msg('请输入邀请码！');
                    return false;
                }
                var JdateJson = {
                    "inviteCode": inviteCode,
                    "loginId": user,
                    "userPassword": pwd
                }
                // return;
                $.ajax({
                    url: 'http://www.xiaofeixiangf.com:8888/v1/client/merchants',
                    type: 'POST',
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(JdateJson),
                    success: function (data) {
                        console.log(data);
                        var _Success = data.code;
                        if (_Success == 200) {
                            // return;
                            layer.msg('注册成功');
                            location.href = "login.html";
                        } else {
                            layer.msg('注册失败');
                        }
                    }
                })
                //防止页面跳转
                return false;
            });
        });
    </script>
</body>

</html>